#include <iostream>

class Solution
{
public:
    static bool isHappy(int n)
    {
        class Solution
        {
        public:
            int bit_sum(int n)
            {
                int sum = 0;
                while (n)
                {
                    int tmp = (n % 10);
                    sum += (tmp * tmp);
                    n /= 10;
                }
                return sum;
            }
            bool isHappy(int n)
            {
                int slow = bit_sum(n), fast = bit_sum(bit_sum(n));
                while (slow != fast)
                {
                    slow = bit_sum(slow);
                    fast = bit_sum(bit_sum(fast));
                }
                return slow == 1;
            }
        };
    }
};

int main()
{
    std::cout << Solution::isHappy(19) << std::endl;
    return 0;
}